package com.ruoyi.gbase.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gbase.domain.GbaseJobInfo;
import com.ruoyi.gbase.domain.GbaseLoadCount;
import com.ruoyi.gbase.domain.TableInfo;
import com.ruoyi.gbase.service.IGbaseJobInfoService;
import com.ruoyi.gbase.service.ITableInfoService;
import com.ruoyi.nsh.util.DateUtil;
import com.ruoyi.gbase.util.GbaseLoadUtil;
import com.ruoyi.gbase.util.GbaseUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * GBASE入库日志Controller
 * 
 * @author yys
 * @date 2020-10-27
 */
@Controller
@RequestMapping("/gbase/jobinfo")
public class GbaseJobInfoController extends BaseController
{
    private static final Logger log = LoggerFactory.getLogger(GbaseJobInfoController.class);
  
    @Autowired
    private IGbaseJobInfoService gbaseJobInfoService;

    @Autowired
    private GbaseUtil gbaseUtil;


    @Autowired
    private ITableInfoService tableInfoService;

    @Autowired
    private GbaseLoadUtil gbaseLoadUtil;



    @PostMapping("/listErrorTable")
    @ResponseBody
    public TableDataInfo getErrorTable()
    {
        startPage();
        String rq = DateUtil.addDay(DateUtils.getdateYYYYMMDD(),-2);
        TableInfo tableInfo = new TableInfo();
        tableInfo.setEtlDate(rq);
        tableInfo.setSendCondition("1");
        tableInfo.setJobRunStatus(4);

        tableInfo.setTableChangeFlag("");
        List<TableInfo> tableInfoList = tableInfoService.selectTableInfoList(tableInfo);

        log.info("当前表的数量"+tableInfoList.size());
        List<TableInfo> tableInfoList2 = new ArrayList<TableInfo>();
        gbaseUtil.connectGBase();
        //循环处理所有的全量入库
        for(TableInfo t1:tableInfoList){
            if(gbaseLoadUtil.zjcf(t1)){
                tableInfoList2.add(t1);
            };
        }

        return getDataTable(tableInfoList2);
    }

    /**
     * 查询GBASE入库日志列表
     * 20211129 yys 开始处理  添加判 拉链表名称 拉链的起始结束日期  拉链表数据量  直接关联gbase去匹配
     */
    @RequiresPermissions("gbase:jobinfo:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(GbaseJobInfo gbaseJobInfo)
    {

     
        startPage();

        List<GbaseJobInfo> list = gbaseJobInfoService.selectGbaseJobInfoList(gbaseJobInfo);

       /* log.info("111  list:"+list.size());
        for(GbaseJobInfo gbaseJobInfo1:list){
            gbaseJobInfo1.setIsLl("0");
            if(gbaseJobInfo1.getSendCondition().equals("0")){
                gbaseJobInfo1.setLlTableName(("lql_"+gbaseJobInfo1.getSchemaName()+"_"+gbaseJobInfo1.getTableName()).toLowerCase());
            }else if(gbaseJobInfo1.getSendCondition().equals("1")){
                gbaseJobInfo1.setLlTableName(("l_"+gbaseJobInfo1.getSchemaName()+"_"+gbaseJobInfo1.getTableName()).toLowerCase());
            }
        }
        try {
            gbaseUtil.connectGBase();
            //获得所有拉链表
            List<String> allLlbiao = llUtil.getAllLl();
            String sb = "";
            //根据名称拼接union all 的查询脚本
            for(String llname:allLlbiao){
                if(llname.substring(0,3).equals("lql")){
                    sb = sb+"select '"+llname+"' name, min(etl_dt) llStartDt,max(etl_dt) llEndDt,count(*) llCount     from datawarehouse."+llname+" \n union all ";
                }else{
                    sb = sb+"select '"+llname+"' name, min(start_dt) llStartDt,max(start_dt) llEndDt,count(*) llCount from datawarehouse."+llname+" \n union all ";
                }
            }
            sb = sb+" select '1' name,'1' llStartDt,'1' llEndDt,0 llCount from cbod.saacnacn  where 1=2  ";

            log.info("querySql:"+sb);
            ResultSet r = gbaseUtil.query(sb);
            while(r.next()) {
                for(GbaseJobInfo gbaseJobInfo1:list){
                    if(gbaseJobInfo1.getLlTableName()==null){
                        continue;
                    }else{
                        if(r.getString(1).equals(gbaseJobInfo1.getLlTableName())){
                            gbaseJobInfo1.setIsLl("1");
                            gbaseJobInfo1.setLlStartDt( r.getString(2));
                            gbaseJobInfo1.setLlEndDt( r.getString(3));
                            gbaseJobInfo1.setLlCount( r.getString(4));
                            break;      //如果取到了 就直接跳出for循环
                        }
                    }
                }
            }
        } catch (Exception e) {
            log.info("出现了异常"+e.getStackTrace());
        }
        if(gbaseJobInfo.getIsLl().equals("1")){
            list = list.stream().filter(s-> s.getIsLl().equals("1")).collect(Collectors.toList());
        }*/

        return getDataTable(list);
    }

    /**
     * 导出GBASE入库日志列表
     */
    @RequiresPermissions("gbase:jobinfo:export")
    @Log(title = "GBASE入库日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GbaseJobInfo gbaseJobInfo)
    {
        List<GbaseJobInfo> list = gbaseJobInfoService.selectGbaseJobInfoList(gbaseJobInfo);
        ExcelUtil<GbaseJobInfo> util = new ExcelUtil<GbaseJobInfo>(GbaseJobInfo.class);
        return util.exportExcel(list, "GBASELOG");
    }

    /**
     * 新增保存GBASE入库日志
     */
    @RequiresPermissions("gbase:jobinfo:add")
    @Log(title = "GBASE入库日志", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GbaseJobInfo gbaseJobInfo)
    {
        return toAjax(gbaseJobInfoService.insertGbaseJobInfo(gbaseJobInfo));
    }


    /**
     * 查询GBASE入库日志列表
     */
    @RequiresPermissions("gbase:jobinfo:list")
    @PostMapping("/listfive")
    @ResponseBody
    public TableDataInfo listfive(GbaseJobInfo gbaseJobInfo)
    {
        startPage();
        String rq = DateUtil.addDay(DateUtils.getdateYYYYMMDD(),-5);
        List<GbaseLoadCount> list = gbaseJobInfoService.selectGbaseLoadCountList3(rq);
        return getDataTable(list);
    }

    /**
     * 查询GBASE入库日志列表
     */
    @PostMapping("/listtoday")
    @ResponseBody
    public TableDataInfo listtoday(GbaseJobInfo gbaseJobInfo)
    {
        startPage();
        String rq = DateUtil.addDay(DateUtils.getdateYYYYMMDD(),-1);
        List<GbaseLoadCount> list = gbaseJobInfoService.selectGbaseLoadCountList4(rq);


        return getDataTable(list);
    }

        /**
         * 查询GBASE入库日志列表
         */
    @PostMapping("/listRQ")
    @ResponseBody
    public TableDataInfo listRQ(String rq)
    {
        List<GbaseLoadCount> list = gbaseJobInfoService.selectGbaseLoadCountList4(rq);
        return getDataTable(list);
    }

    @PostMapping("/loadshow")
    @ResponseBody
    public AjaxResult loadshow()
    {
        String rq = DateUtil.addDay(DateUtils.getdateYYYYMMDD(),-1);
        GbaseLoadCount gbaseJobInfo = gbaseJobInfoService.selectGbaseLoadCount(rq);

        AjaxResult ajax = AjaxResult.success();
        ajax.put("rq", rq);
        if(gbaseJobInfo==null){
            ajax.put("dbnums", 0);
            ajax.put("loadnums", 0);
        }else{
            //log.info("下发数据量:"+gbaseJobInfo.getDbnums() +" 已经入库量:"+gbaseJobInfo.getGbasenums());
            ajax.put("dbnums", gbaseJobInfo.getDbnums());
            ajax.put("loadnums", gbaseJobInfo.getGbasenums());
        }
        return ajax;
    }

    /**
     * 修改保存GBASE入库日志
     */
    @RequiresPermissions("gbase:tableinfo:edit")
    @Log(title = "GBASE入库日志", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GbaseJobInfo gbaseJobInfo)
    {
        return toAjax(gbaseJobInfoService.updateGbaseJobInfo(gbaseJobInfo));
    }

}
